﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class main : System.Web.UI.Page
{
    private int perPage = 10;
    protected void Page_Load(object sender, EventArgs e)
    {
         
        if (!IsPostBack)
        {
            LoadData(0 * perPage, perPage);
            LBtnNext.PostBackUrl = "?page=2";
            long id = Convert.ToInt64(Request.QueryString["id"]);

        }
    }
    /// <summary>
    /// 加载首页事件监听
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void LBtn_Click_First(object sender, EventArgs e)
    {
        LoadData(0 * perPage, perPage);
    }
    /// <summary>
    /// 加载下一页事件监听
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void LBtn_Click_Next(object sender, EventArgs e)
    {
        
        int page = Convert.ToInt32(Request.QueryString["page"]);
        
            LoadData((page - 1) * perPage, perPage);
            LBtnNext.PostBackUrl = "?page=" + (page + 1);
        
       
    }
    /// <summary>
    /// 加载上一页事件监听
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void LBtn_Click_Last(object sender, EventArgs e)
    {
        
        int page = Convert.ToInt32(Request.QueryString["page"]);
        if (page > 1)
        {
            LoadData((page - 2) * perPage, perPage);
            LBtnNext.PostBackUrl = "?page=" + (page - 1);
        }
        else
        {
            LoadData(0 * perPage, perPage);
        }
    }
    /// <summary>
    /// 加载数据库数据
    /// </summary>
    /// <param name="start"></param>
    /// <param name="num"></param>
    private void LoadData(int start, int num)
    {

        try
        {
            Literal1.Text = "<table>\n";
            Literal1.Text += "<tr>\n";
            Literal1.Text += "<th>序号</th>\n";
            Literal1.Text += "<th>姓名</th>\n";
            Literal1.Text += "<th>课程</th>\n";
            Literal1.Text += "<th>分数</th>\n";
            Literal1.Text += "<tr>\n";
            foreach (var data in GradeDao.FindAll(start, num))
            {
                Literal1.Text += "<tr>\n";
                Literal1.Text += "<td>" + data.Id + "</td>\n";
                Literal1.Text += "<td>" + data.Studentname + "</td>\n";
                Literal1.Text += "<td>" + data.Coursename + "</td>\n";
                Literal1.Text += "<td>"+ data.Score +"</td>\n";
                Literal1.Text += "<tr>\n";
            }
            Literal1.Text += "</table>\n";
        }
        catch (Exception)
        {
            Literal1.Text = "数据加载异常!";
        }
    }
    /// <summary>
    /// 查询事件监听
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void BtnQuery_Click(object sender, EventArgs e)
    {
        if (TbName.Text != null)
        {
            string studentname = TbName.Text;
            var data = GradeDao.Find(studentname);
            TbCourse.Text = data.Coursename;
            TbScore.Text = data.Score.ToString();
        }
        else
        {
            Literal2.Text = "请先输入姓名!";
        }
        
    }
    /// <summary>
    /// 修改事件监听
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void BtnUpdate_Click(object sender, EventArgs e)
    {
        string studentname = TbName.Text;
        string coursename = TbCourse.Text;
        update(studentname,coursename);
    }
    /// <summary>
    /// 添加事件监听
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void BtnAdd_Click(object sender, EventArgs e)
    {
        AddInfo();
    }
    /// <summary>
    /// 删除事件监听
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void BtnDelete_Click(object sender, EventArgs e)
    {
        string studentname = TbName.Text;
        var data = GradeDao.Find(studentname);
        long id = data.Id;
        delete(id);
    }
    /// <summary>
    /// delete方法
    /// </summary>
    /// <param name="name"></param>
    private void delete(long id)
    {
        GradeDao.Delete(id);
    }
    /// <summary>
    /// 查询是否有数据
    /// </summary>
    /// <param name="studentname">姓名</param>
    /// <param name="coursename">课程名</param>
    /// <returns></returns>
    private bool IsExist(string studentname, string coursename)
    {
        return GradeDao.IsExist(studentname,coursename);
    }
    /// <summary>
    /// 修改数据
    /// </summary>
    /// <param name="studentname">姓名</param>
    /// <param name="coursename">课程名</param>
    private void update(string studentname,string coursename)
    {

        Grade data = GradeDao.Find(studentname);
        TbName.Text = data.Studentname;
        TbCourse.Text = data.Coursename;
        TbScore.Text= data.Score.ToString();
        HfId.Value = data.Id.ToString();
        SaveInfo();
    }
    /// <summary>
    /// 保存数据
    /// </summary>
    private void SaveInfo()
    {
        string name = TbName.Text.Trim();
        Grade data = GradeDao.Find(name);
        data.Studentname = TbName.Text.Trim();
        data.Coursename = TbCourse.Text.Trim();
        GradeDao.Update(data);

    }
    /// <summary>
    /// 插入数据
    /// </summary>
    private void AddInfo()
    {
        Grade data = new Grade();
        data.Studentname = TbName.Text.Trim();
        data.Coursename= TbCourse.Text.Trim();
        long insertId = GradeDao.Insert(data);
        Debug.WriteLine("插入数据返回Id:" + insertId);
    }
}